package com.example.androidexample.surfaceview

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.SurfaceHolder
import android.view.SurfaceView
import kotlin.math.abs

class SurfaceViewDemo : SurfaceView,SurfaceHolder.Callback, Runnable {

    constructor(context: Context) : super(context) {
        initView()
    }

    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
        initView()
    }

    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ) {
        initView()
    }

    lateinit var surfaceHolder: SurfaceHolder
    lateinit var canvas: Canvas
    //子线程标志位
    var  isDrawing = false;

    // 画笔
    var mPaint: Paint? = null

    // 路径
    var mPath: Path? = null
    //上次的坐标
    private var mLastX = 0f
    private var mLastY = 0f

    private fun initView() {
        surfaceHolder = holder
        //注册回调方法
        surfaceHolder.addCallback(this)
        //设置一些参数方便后面绘图
        isFocusable = true
        keepScreenOn = true
        isFocusableInTouchMode = true

        //画笔
        //画笔
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
        mPaint!!.strokeWidth = 10f
        mPaint!!.color = Color.parseColor("#FF4081")
        mPaint!!.style = Paint.Style.STROKE
        mPaint!!.strokeJoin = Paint.Join.ROUND
        mPaint!!.strokeCap = Paint.Cap.ROUND
        //路径
        mPath = Path()
    }

    override fun surfaceCreated(holder: SurfaceHolder) {
        isDrawing = true
        Thread(this).start()
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
        //改变
    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {
        //销毁
        isDrawing = false
    }

    override fun run() = synchronized(isDrawing) {
        //子线程
        while (isDrawing) {
            drawing();
        }
    }

    private fun drawing() {
        try {
            canvas = surfaceHolder.lockCanvas()

            canvas.drawColor(Color.WHITE);
            canvas.drawPath(mPath!!, mPaint!!);

        } finally {
            surfaceHolder.unlockCanvasAndPost(canvas)
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        val x = event!!.x
        val y = event.y
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                mLastX = x
                mLastY = y
                mPath!!.moveTo(mLastX, mLastY)
            }
            MotionEvent.ACTION_MOVE -> {
                val dx = abs(x - mLastX)
                val dy = abs(y - mLastY)
                if (dx >= 3 || dy >= 3) {
                    mPath!!.quadTo(mLastX, mLastY, (mLastX + x) / 2, (mLastY + y) / 2)
                }
                mLastX = x
                mLastY = y
            }
            MotionEvent.ACTION_UP -> {
            }
        }
        return true
    }

    /**
     * 测量
     */
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val wSpecMode = MeasureSpec.getMode(widthMeasureSpec)
        val wSpecSize = MeasureSpec.getSize(widthMeasureSpec)
        val hSpecMode = MeasureSpec.getMode(heightMeasureSpec)
        val hSpecSize = MeasureSpec.getSize(heightMeasureSpec)
        if (wSpecMode == MeasureSpec.AT_MOST && hSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(300, 300)
        } else if (wSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(300, hSpecSize)
        } else if (hSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(wSpecSize, 300)
        }
    }

}